#include "progressbar.h"
#include "ui_progressbar.h"


int const plusTime = 300; // time for "+5" button in sec

progressbar::progressbar(QWidget *parent) :
  QWidget(parent),
  ui(new Ui::progressbar)
{
  ui->setupUi(this);
  Proc = new Process (this);
  this->setWindowFlags(Qt::Window
                           | Qt::WindowStaysOnTopHint
                           | Qt::X11BypassWindowManagerHint
                           | Qt::FramelessWindowHint);

  setAttribute(Qt::WA_InputMethodTransparent, true);
  setAttribute(Qt::WA_TranslucentBackground, true);
  setAttribute(Qt::WA_TransparentForMouseEvents, true);

  ui->progressBar->setStyleSheet(QString("QProgressBar:horizontal { border: 0px solid gray;  border-radius: 3px; background: transparent;padding: 1px; }   QProgressBar::chunk:horizontal { background: qlineargradient(x1: 0, y1: 0.5, x2: 1, y2: 0.5, stop: 0 green, stop: 1 white);}"));
  heightPbar=ui->progressBar->height();
  ui->horizontalPbarSpacer->changeSize(QApplication::desktop()->screen()->width()*0.3,10);

  refreshTimer = new QTimer(this);
  refreshSpeed=1000; //refresh speed in msec
  middleStatus=0; //when change color to yellow
  lowStatus=0; //when change color to red
  connect(this, SIGNAL(changedProgressBarValue()), this, SLOT(setProgressBarStatus()));
  connect(Proc, SIGNAL(finished(int)), this, SLOT(procTerminated(int)));

}

progressbar::~progressbar()
{
  delete refreshTimer;
  delete ui;
  delete Proc;
}

void progressbar::minusProgressBarValue()
{

  if (ui->progressBar->value()!=0)
    {
      ui->progressBar->setValue(ui->progressBar->value()-1);
      changedProgressBarValue();
      ui->currentTime->setText(QString::number(ui->progressBar->value()/60+1));

      refreshTimer->start(refreshSpeed);
    }
  else  //time is over
    {
      this->setClearProgressBar();  //set default progressbar
      Proc->terminate();
      Proc->waitForFinished();
      this->hide();
      timerIsOver();
    }
}

void progressbar::on_buttonClose_clicked()
{
  Proc->terminate();
  Proc->waitForFinished();
  this->setClearProgressBar();  //set default progressbar
  this->hide();
  clickedCloseButton();

}

void progressbar::on_buttonNext_clicked()
{
  Proc->terminate();
  Proc->waitForFinished();
  this->setClearProgressBar();  //set default progressbar
  this->hide();
  clickedNextButton();
}

void progressbar::on_buttonIncrement_clicked()
{
    ui->progressBar->setMaximum (ui->progressBar->maximum () + plusTime);
    ui->progressBar->setValue (ui->progressBar->value () + plusTime);
    ui->currentTime->setText(QString::number(ui->progressBar->value()/60+1));
    changedProgressBarValue ();
    clickedIncrementButton ();
}

void progressbar::setProgressBarOpacity(qreal value=1.0)
{
  ui->progressBar->setWindowOpacity(value);
  changedProgressBarOpacity();
}

void progressbar::setProgressBarColor(QString color)
{
  ui->progressBar->setStyleSheet(QString("QProgressBar:horizontal { border: 0px solid gray;  border-radius: 3px; background: transparent;padding: 1px; }   QProgressBar::chunk:horizontal { background: qlineargradient(x1: 0, y1: 0.5, x2: 1, y2: 0.5, stop: 0 "+color+ ", stop: 1 white);}"));
  changedProgressBarColor();
}


void progressbar::setProgressBarStatus()
{

  if(ui->progressBar->value() <= middleStatus && ui->progressBar->value() > lowStatus)
    {
      this->setProgressBarColor("yellow");
      this->changedProgressBarStatus();
    }
  else if(ui->progressBar->value() <= lowStatus)
    {
      this->setProgressBarColor("red");
      this->changedProgressBarStatus();
    }
  else if(ui->progressBar->value() > middleStatus)
    {
      this->setProgressBarColor("green");
    }
}

bool progressbar::startWork(unsigned int time, QString type, QString FPath)
{
  if (time!=0)
    {
      if(type!="Presentation")
        {
          ui->labelPic->setPixmap(QPixmap(FPath));
        }
      else
        {
          Proc->startPresentation(FPath);
        }


      time=time*60;

      ui->progressBar->setMaximum(time);
      ui->progressBar->setValue(time);
      middleStatus=time/2;
      lowStatus=time*0.05;
      connect(this->refreshTimer, SIGNAL(timeout()), this, SLOT(minusProgressBarValue()));
      this->showFullScreen();
      refreshTimer->start(refreshSpeed);
      return true;
    }
  else
    {
      return false;
    }
}

void progressbar::setClearProgressBar()
{
  disconnect(this->refreshTimer, SIGNAL(timeout()), this, SLOT(minusProgressBarValue()));
  this->refreshTimer->stop();
  ui->progressBar->setValue(0);
  ui->progressBar->setMaximum(1);
  middleStatus=0;
  lowStatus=0;
  ui->labelPic->clear();
  this->setProgressBarColor("green");

}

void progressbar::procTerminated(int)
{
  ui->labelPic->setPixmap(QPixmap(qApp->applicationDirPath () + "/presentation/_break.jpg"));
  ui->labelPic->show();
}
